/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.common;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.fresco.samples.showcase.drawee.DraweeScaleTypeAbility;
import ohos.agp.components.Text;
import ohos.agp.utils.Point;
import ohos.agp.window.dialog.ListDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SimpleScaleTypeDialog {

    public interface onSelectListener{
        void onSelect(int index, Entry entry);
    }
    public static class Entry {
        public final ScalingUtils.ScaleType scaleType;
        public final String description;
        public final Point focusPoint;

        private Entry(ScalingUtils.ScaleType scaleType, String description, Point focusPoint) {
            this.scaleType = scaleType;
            this.description = description;
            this.focusPoint = focusPoint;
        }
    }
    private static final Entry[] BUILT_IN_SPINNER_ENTRIES =
            new Entry[] {
                    new Entry(ScalingUtils.ScaleType.CENTER, "center", null),
                    new Entry(ScalingUtils.ScaleType.CENTER_CROP, "center_crop", null),
                    new Entry(ScalingUtils.ScaleType.CENTER_INSIDE, "center_inside", null),
                    new Entry(ScalingUtils.ScaleType.FIT_CENTER, "fit_center", null),
                    new Entry(ScalingUtils.ScaleType.FIT_START, "fit_start", null),
                    new Entry(ScalingUtils.ScaleType.FIT_END, "fit_end", null),
                    new Entry(ScalingUtils.ScaleType.FIT_XY, "fit_xy", null),
                    new Entry(ScalingUtils.ScaleType.FOCUS_CROP, "focus_crop (0, 0)", new Point(0, 0)),
                    new Entry(ScalingUtils.ScaleType.FOCUS_CROP, "focus_crop (1, 0.5)", new Point(1, 0.5f))
            };

    private static final Entry[] CUSTOM_TYPES =
            new Entry[] {
                    new Entry(CustomScaleTypes.FIT_X, "custom: fit_x", null),
                    new Entry(CustomScaleTypes.FIT_Y, "custom: fit_y", null),
            };

    public static void createDialog(Context context, int selectIndex, onSelectListener listener){
        List<Entry> entries = new ArrayList<>(BUILT_IN_SPINNER_ENTRIES.length + CUSTOM_TYPES.length);
        Collections.addAll(entries, BUILT_IN_SPINNER_ENTRIES);
        Collections.addAll(entries, CUSTOM_TYPES);
        new ListDialog(context){
            {
                String[] items = new String[entries.size()];
                entries.stream().flatMap( entry -> Stream.of(entry.description)).collect(Collectors.toList()).toArray(items);
                setSingleSelectItems(items,selectIndex);
                setOnSingleSelectListener((iDialog, i) -> {
                    if(listener !=null){
                        listener.onSelect(i,entries.get(i));
                    }
                    /*
                    mSelectIdx = i;
                    Entry entry = entries.get(i);
                    changeMainDraweeScaleType(entry.scaleType,entry.focusPoint);
                    mSpinner.setText(entry.description);

                     */
                    iDialog.destroy();
                });
            }
        }.show();

    }
}
